package com.example.internet;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import java.text.SimpleDateFormat;
import java.util.Date;


public class ActivityAdd extends AppCompatActivity {
TextView tvSave;
EditText edNum;
EditText edState1;
EditText edState2;
TextView tvDel;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add);
        tvSave = findViewById(R.id.tv_save);
        edNum = findViewById(R.id.ed_num);
        edState1 = findViewById(R.id.ed_state1);
        edState2 = findViewById(R.id.ed_state2);
        tvDel = findViewById(R.id.tv_del);

        Intent intent = getIntent();               //得到其他页面传过来的intent对象
        final String type = intent.getStringExtra("type");  // 用来判断是修改或者添加

        String num =  intent.getStringExtra("num");
        String state1 = intent.getStringExtra("state1");
        String state2 = intent.getStringExtra("state2");
                                                   //为保存按钮设置监听事件 如果是添加 进行添加操作
        tvSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (type.equals("添加"))
                add();
                if(type.equals("修改")){
                       updata();
                }
            }
        });

        edNum.setText(num);
        edState1.setText(state1);
        edState2.setText(state2);
                                                        //为删除按钮设置监听事件 点击后执行del方法
        tvDel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                del();
            }
        });
    }

    private void del() {

        SqlUse use = new SqlUse(ActivityAdd.this);
        use.detele(edNum.getText().toString());
        Toast.makeText(ActivityAdd.this,"删除成功！",Toast.LENGTH_SHORT).show();
        finish();
    }

    private void add() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");// HH:mm:ss
        //获取当前时间
        Date date = new Date(System.currentTimeMillis());

        Computet computet= new Computet(simpleDateFormat.format(date),
               edNum.getText().toString(),edState1.getText().toString(),
                edState2.getText().toString()) ;
        SqlUse use = new SqlUse(ActivityAdd.this);
        use.add(computet);
        Toast.makeText(ActivityAdd.this,"添加成功！",Toast.LENGTH_SHORT).show();
        finish();
    }
    private void updata(){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");// HH:mm:ss
        Date date = new Date(System.currentTimeMillis());

        //创建Computet对象 利用Computet对象传入参数 ，进行修改
        Computet computet= new Computet(simpleDateFormat.format(date),
                edNum.getText().toString(),edState1.getText().toString(),
                edState2.getText().toString());
        SqlUse use = new SqlUse(ActivityAdd.this);
        use.updata(computet);
        Toast.makeText(ActivityAdd.this,"修改成功！",Toast.LENGTH_SHORT).show();
        finish();
    }
}